﻿using System;
using System.Threading.Tasks;
using Antares.VIEWs;
using AntaresShell.Localization;
using AntaresShell.NavigatorProvider;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Antares
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage
    {
        private readonly List<SettingsCommand> _commands;
        internal Rect SplashImageRect;
        internal bool Dismissed;
        public Frame RootFrame
        {
            get { return _rootFrame; }
            private set { _rootFrame = value; }
        }

        public MainPage()
        {
            InitializeComponent();
            SnapGrid.Loaded += SnapViewOnLoaded;
            SizeChanged += OnSizeChanged;

            Navigator.Instance.MainPopup = PopupMenu;
            Navigator.Instance.MainProgressBar = PrgBar;
            Navigator.Instance.RootFrameOfPopup = RootFrameOfPopup;
            Navigator.Instance.BottomAppBar = BottomAppBar;
            Navigator.Instance.TopAppBar = TopAppBar;
            Navigator.Instance.SetNotificator(networkStatus);
            //Navigator.Instance.SetApproveNotificator(notification);
            SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequestedHandler;
            _commands = new List<SettingsCommand> 
                            { 
                                 new SettingsCommand(LanguageProvider.Resource["MainTitle_9"], LanguageProvider.Resource["MainTitle_9"], AboutCommandHandler),
                                 new SettingsCommand(LanguageProvider.Resource["MainTitle_7"], LanguageProvider.Resource["MainTitle_7"], SettingCommandHandler),
                                 new SettingsCommand(LanguageProvider.Resource["MainTitle_10"], LanguageProvider.Resource["MainTitle_10"], PolicyCommandHandler),
                                 new SettingsCommand(LanguageProvider.Resource["MainTitle_11"], LanguageProvider.Resource["MainTitle_11"], HelpCommandHandler),
                            };

            networkStatus.Tapped += networkStatus_Tapped;
            networkStatus.PointerEntered += (sender, args) =>
                                                {
                                                    networkStatus.Opacity = 1;
                                                };

            networkStatus.PointerExited += (sender, args) =>
            {
                networkStatus.Opacity = .85;
            };
            // AppButtonManager.Instance.GridAppbar = GridAppbar;

            // AppButtonManager.Instance.AddTaskBtn = AddNewTaskBtn;
            // //AppButtonManager.Instance.ViewTaskBtn = ViewTaskDetail;
            //// AppButtonManager.Instance.DelTaskBtn = DeleteMember;

            // AppButtonManager.Instance.AddProjectBtn = AddNewTaskBtn;
            // //AppButtonManager.Instance.ViewProjectBtn = ViewProjectDetail;
            //// AppButtonManager.Instance.DelProjectBtn = DeleteProject;

            // //AppButtonManager.Instance.GridAppbar.Children.Remove(AppButtonManager.Instance.ViewTaskBtn);
            // AppButtonManager.Instance.GridAppbar.Children.Remove(AppButtonManager.Instance.DelTaskBtn);
            // //AppButtonManager.Instance.GridAppbar.Children.Remove(AppButtonManager.Instance.ViewProjectBtn);
            // AppButtonManager.Instance.GridAppbar.Children.Remove(AppButtonManager.Instance.DelProjectBtn);
        }

        private async void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            PopupMenu.Width = e.NewSize.Width;
            PopupMenu.Height = e.NewSize.Height;
            RootGridOfPopup.Width = e.NewSize.Width;
            RootGridOfPopup.Height = e.NewSize.Height;
            RootFrame.Width = e.NewSize.Width;

            RootFrame.Visibility = Visibility.Collapsed;
            var viewState = ApplicationView.Value;

            if (viewState == ApplicationViewState.Snapped)
            {
                TopAppBar.IsOpen = false;
                SnapGrid.Visibility = Visibility.Visible;
            }
            else
            {
                await Task.Delay(30);
                RootFrame.Width = Window.Current.Bounds.Width;
                SnapGrid.Visibility = Visibility.Collapsed;
                RootFrame.Visibility = Visibility.Visible;
            }

            UpdateLayout();
        }

        private void SnapViewOnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            SnapFrame.Navigate(typeof(SnapView));
        }

        public void SetExtendedSplashInfo(Rect splashRect, bool dismissStat)
        {
            SplashImageRect = splashRect;
            Dismissed = dismissStat;
        }

        void networkStatus_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            Navigator.Instance.NavigateTo(typeof(HelpPage));
        }

        /// <summary>
        /// Event occur request of command handler.
        /// </summary>
        /// <param name="command"> Not param.</param>
        private void PolicyCommandHandler(IUICommand command)
        {
            Navigator.Instance.ShowPopup(typeof(PrivacyPolicyPage));
        }

        /// <summary>
        /// Event occur request of command handler.
        /// </summary>
        /// <param name="command"> Not param.</param>
        private void HelpCommandHandler(IUICommand command)
        {
            Navigator.Instance.NavigateTo(typeof(HelpPage));
        }

        /// <summary>
        /// Event occur request of command handler.
        /// </summary>
        /// <param name="command"> Not param.</param>
        private void SettingCommandHandler(IUICommand command)
        {
            Navigator.Instance.ShowPopup(typeof(SettingPage));
        }

        /// <summary>
        /// Event occur request of command handler.
        /// </summary>
        /// <param name="command"> Not param.</param>
        private void AboutCommandHandler(IUICommand command)
        {
            Navigator.Instance.ShowPopup(typeof(AboutPage));
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (ApplicationView.Value != ApplicationViewState.Snapped)
            {
                Navigator.Instance.NavigateTo(typeof(TimelineWeekPage));
            }
            else
            {
                SnapGrid.Visibility = Visibility.Visible; 
            }
        }

        private void CommandsRequestedHandler(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            foreach (var command in _commands.Where(command => !args.Request.ApplicationCommands.Contains(command)))
            {
                args.Request.ApplicationCommands.Add(command);
            }
        }
    }
}
